package com.qf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.entity.User;
import com.qf.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.management.QueryEval;
import java.util.Date;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/addUser")
    public Boolean addUser(@RequestBody User user) {
//        user.setId(""); // 当前登录的id
        user.setCreateTime(new Date());
        return userService.save(user);
    }

    @GetMapping("/getUserList")
    public List<User> getUserList() {
        List<User> list = userService.list();
        System.out.println(list);
        return list;
    }

    @PostMapping("/queryUserList")
    public List<User> queryUserList(@RequestBody User user) {

        // 1.创建一个查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(user.getUsername())) {
            wrapper.like("username", user.getUsername());
        }
        if (!StringUtils.isEmpty(user.getEmail())) {
            wrapper.eq("email", user.getEmail());
        }

        // 2.根据查询条件查询数据
        return userService.list(wrapper);
    }


    @GetMapping("/getUserByName")
    public User getUserByName(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        return userService.getOne(wrapper);
    }

    @GetMapping("/getUserByOauthId")
    public User getUserByOauthId(String uuid){
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("oauth_id", uuid);
        return userService.getOne(wrapper);
    }
}
